package cc.zy.base.businesses.controller;

import cc.zy.base.businesses.entity.NetworkSchool;
import cc.zy.base.businesses.entity.NetworkSchoolExcel;
import cc.zy.base.businesses.service.INetworkSchoolService;
import cc.zy.base.common.annotation.ControllerEndpoint;
import cc.zy.base.common.controller.BaseController;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.exception.FebsException;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 233网校
 *
 * @Author hutengjiao
 * @Date: 2021/4/19 14:09
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
@RequestMapping("networkSchool")
public class NetworkSchoolController extends BaseController {

    private final INetworkSchoolService networkSchoolService;

    /**
     * 233网校账号密码页面展示
     *
     * @Author hutengjiao
     * @Date: 2021/4/19 14:09
     * @param request, notice
     * @return cc.zy.base.common.entity.FebsResponse
     */
    @GetMapping("list")
    @ResponseBody
    public FebsResponse networkSchoolList(QueryRequest request, NetworkSchool networkSchool) {

        log.debug(String.format("networkSchool = %s", networkSchool));

        Map<String, Object> dataTable = getDataTable(networkSchoolService.listNetworkSchool(request, networkSchool));
        log.debug(String.format("dataTable = %s", dataTable));

        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 获取报名费列表
     *
     * @Author hutengjiao
     * @Date: 2021/4/19 16:40
     * @param
     * @return cc.zy.base.common.entity.FebsResponse
     */
    @GetMapping("originalPrice")
    @ResponseBody
    public FebsResponse getOriginalPrice() {

        log.debug("获取报名费列表");

        List<Map<String, Object>> price = networkSchoolService.findPrice();
        log.debug(String.format("price = %s", price));

        return new FebsResponse().success().data(price);
    }

    /**
     * 下载模板
     *
     * @Author hutengjiao
     * @Date: 2021/4/19 18:15
     * @param response
     * @return void
     */
    @GetMapping("downloadNetwork")
    public void dowmloadNetwork(HttpServletResponse response){

        log.debug(String.format(" response = %s", response));

        List<NetworkSchoolExcel> temp = new ArrayList<>();

        NetworkSchoolExcel networkSchoolExcel1 = new NetworkSchoolExcel();
        networkSchoolExcel1.setUsername("XXXXXX");
        networkSchoolExcel1.setPassword("XXXXXX");
        networkSchoolExcel1.setActivate("已激活");

        NetworkSchoolExcel networkSchoolExcel2 = new NetworkSchoolExcel();
        networkSchoolExcel2.setUsername("XXXXXX");
        networkSchoolExcel2.setPassword("XXXXXX");
        networkSchoolExcel2.setActivate("未激活");

        temp.add(networkSchoolExcel1);
        temp.add(networkSchoolExcel2);

        // 构建模板
        ExcelKit.$Export(NetworkSchoolExcel.class, response).downXlsx(temp, true);
    }

    /**
     * 导入Excel数据，并批量插入 T_EXIMPORT表
     */
    @PostMapping("import")
    @ControllerEndpoint(exceptionMessage = "导入Excel数据失败")
    @ResponseBody
    public FebsResponse importNetworkSchoolExcels(MultipartFile file, HttpSession session) {

        log.debug(String.format("传入的文件流 file = %s", file));
        log.debug(String.format("失败日志的唯一标识 unique = %s", session.getAttribute("unique")));

        if (file.isEmpty()) {
            throw new FebsException("导入数据为空");
        }
        String filename = file.getOriginalFilename();
        if (!StringUtils.endsWith(filename, ".xlsx")) {
            log.error("只支持.xlsx类型文件导入");
            throw new FebsException("只支持.xlsx类型文件导入");
        }

        try {
            String code = networkSchoolService.readExcel(file, session);
            log.debug(String.format("返回的导入状态值 code = %s", code));
            return new FebsResponse().success().message(code);
        } catch (Exception e) {
            String code="500";
            log.error(e.toString());
            log.debug(String.format("返回的导入状态值 code = %s", code));
            return new FebsResponse().success().message(code);
        }

    }

    /**
     * 导出信息
     *
     * @return
     * @author wangpin
     * @date 2021/02/01
     */
    @GetMapping("excel")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败")
    public void export(QueryRequest request, HttpServletResponse response, NetworkSchool networkSchool) {

        log.debug(String.format(" request = %s", request));
        log.debug(String.format(" response = %s", response));
        log.debug(String.format(" networkSchool = %s", networkSchool.getUsername()));

        List<NetworkSchool> allNetwork = networkSchoolService.listNetwork(networkSchool);
        log.debug(String.format("allNetwork = %s", allNetwork));

        ExcelKit.$Export(NetworkSchool.class, response).downXlsx(allNetwork, false);
    }

    /**
     * 重置账号信息
     *
     * @Author hutengjiao
     * @Date: 2021/4/21 14:22
     * @param username
     * @return cc.zy.base.common.entity.FebsResponse
     */
    @GetMapping("resetNetworkSchool")
    @ResponseBody
    public FebsResponse resetUsername(String username){

        log.debug(String.format("username = %s", username));

        Boolean aBoolean = networkSchoolService.resetNetworkSchool(username);

        if (aBoolean){
            return new FebsResponse().success();
        }else {
            return new FebsResponse().fail();
        }
    }

    /**
     * 添加备注
     *
     * @Author hutengjiao
     * @Date: 2021/4/21 15:14
     * @param remark, username
     * @return cc.zy.base.common.entity.FebsResponse
     */
    @GetMapping("remark")
    @ResponseBody
    public FebsResponse addRemark(String remark, String username){

        log.debug(String.format("remark = %s", remark));
        log.debug(String.format("username = %s", username));

        Boolean addremark = networkSchoolService.addremark(remark, username);

        if (addremark){
            return new FebsResponse().success();
        }else {
            return new FebsResponse().fail();
        }
    }
}
